$(document).ready(function(){
	$(".searchtab table tbody tr").remove();
	$(".ADsearch").click(function(){
		var fromCity = $(".fromCity").val();
		var toCity = $(".toCity").val();
		var start=$(".DRdate input").val();
		var end=$(".DRdate2 input").val();
		searchUserList(fromCity,toCity,start,end);
	}); 
	$.ajax({ 
        type: "GET",
        url: "./GetCityNames", 
        data:'' ,
        success: function(json){
        	var msg =JSON.parse(json);        	
        	$(".toCity option").remove();
        	$(".fromCity option").remove();
        	var html="";
        	if(msg!=null && msg.data.length>0){
                for(var i = 0; i< msg.data.length;i++){
                	var city = msg.data[i];                	
                	html+="<option value='"+city.CityName+"' >"+city.CityName+"</option>";
                }
        	}
        	$(".toCity").append(html);
        	$(".fromCity").append(html);
        }
	});
	
	$(".subCancel").click(function(){
		if($(".searchtab table tbody tr.tdselectedcolor").length>0){
			var scheduleId = $(".searchtab table tbody tr.tdselectedcolor:eq(0)").attr("scheduleId");
			var status=$(".searchtab table tbody tr.tdselectedcolor td:eq(8)").text();
			alert("当前航班计划Id为："+scheduleId);
			if(status=="Confirmed"){
				status="Canceled";
			}
			else{
				status="Confirmed";
			}
			var ajaxdata="scheduleId="+scheduleId+"&status="+status;
			$.ajax({ 
		        type: "GET",
		        url: "./UpdateSchedule", 
		        data:ajaxdata ,
		        success: function(json){
		        	var msg =JSON.parse(json);
		        	if(msg.flag=="success"){
		        		var fromCity = $(".fromCity").val();
		        		var toCity = $(".toCity").val();
		        		var start=$(".DRdate input").val();
		        		var end=$(".DRdate2 input").val();
		        		searchUserList(fromCity,toCity,start,end);
		        	}
		        }
			});
		}
		else{
			alert("您没有选择数据");
		}
	});
});

function searchUserList(fromCity,toCity,startDate,endDate){
	var ajaxdata="fromCity="+fromCity+"&toCity="+toCity+"&startDate="+startDate+"&endDate="+endDate;
	$.ajax({ 
        type: "GET",
        url: "./GetSchedule", 
        data:ajaxdata ,
        success: function(json){
        	var msg =JSON.parse(json);
        	$(".searchtab table tbody tr").remove();
        	var html="";
        	if(msg!=null && msg.infoList.length>0){
                for(var i = 0; i< msg.infoList.length;i++){
                	var schedule = msg.infoList[i];
                	var tr="<tr class='tdcolor1' onclick='trclick(this)' scheduleId='"+schedule.scheduleId+"'>" +
                	"<td>"+schedule.date+"</td>" +
                	"<td>"+schedule.time+"</td>" +
        			"<td>"+schedule.departcityname+"/"+schedule.departcitycode+"</td>" +
        			"<td>"+schedule.arrivecityname+"/"+schedule.arrivecitycode+"</td>" +
        			"<td>"+schedule.name+"</td>" +
        			"<td>"+schedule.EconomyPrice+"</td>" +
        			"<td>"+schedule.FlightNumber+"</td>" +
        			"<td>"+schedule.Gate+"</td>" +
        			"<td>"+schedule.status+"</td>" +
        			"<td onclick='tdClick(this)'>detail</td>" +
        			"</tr>";
                	html+=tr;
                }
        	}
        	$(".searchtab table tbody").append(html);
        	$(".searchtab table tbody tr:odd").removeClass("tdcolor1").addClass("tdcolor");
        } 
     });
}

function trclick(obj){
	$(".searchtab table tbody tr").removeClass("tdselectedcolor");
	$(obj).addClass("tdselectedcolor");
}

function tdClick(obj){
	var scheduleId = $(obj).parent().attr("scheduleId"); 
	localStorage.setItem("scheduleId",scheduleId);
	location.href="TicketSalesDetail.html";
}